﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.ComponentModel.DataAnnotations.Schema;
using System.ComponentModel.DataAnnotations;
using RooHui.Infrastructure.Entities.EFContext;
using RooHui.Infrastructure.Entities.Repository;
using RooHui.Infrastructure.Entities.Enum;

namespace RooHui.Infrastructure.Entities.Entity
{
    public class Project : IAccessible<P_Project, Project>, ILogicEntity<P_Project, Project>
    {
        public int ID { get; set; }
        public string BorrowerName { get; set; }
        public string ProjectTitle { get; set; }
        public string ProjectCode { get; set; }
        public int ProductID { get; set; }
        public string ProductName { get; set; }
        public int EnterpriseID { get; set; }
        public string ProjectDescription { get; set; }
        public decimal Financing { get; set; }
        public decimal MinInvestment { get; set; }
        public decimal MaxInvestment { get; set; }
        public decimal IncreaseMoney { get; set; }
        public DateTime BeginDate { get; set; }
        public DateTime EndDate { get; set; }
        public int CycleAmount { get; set; }
        public DateTime ReleaseTime { get; set; }
        public DateTime FullScaleTime { get; set; }
        public DateTime LendingTime { get; set; }
        public DateTime BorrowInterestDate { get; set; }
        public int WaitDay { get; set; }
        public int GuaranteeCompanyID { get; set; }
        public string CollateralOpinion { get; set; }
        public string RiskCotrolSummary { get; set; }
        public ProjectStatus Status { get; set; }
        public string CreateUser { get; set; }
        public DateTime CreateTime { get; set; }

        public decimal OriginalRate { get; set; }
        public decimal GuaranteeRate { get; set; }

        public Product Product { get; set; }
        public Enterprise Enterprise { get; set; }
        public GuaranteeCompany GuaranteeCompany { get; set; }
        public ProjectPolicy ProjectPolicy { get; set; }
        public Policy Policy { get; set; }
        public List<Policy> PolicyList { get; set; }
        public List<Resource> ResourceList { get; set; }
        public User User { get; set; }
        public Member BorrowerUser { get; set; } 

        public Project LoadUser(U_User u_user)
        {
            this.User = new User().Set(u_user);
            return this;
        }

        public Project LoadBorrowerUser(P_Member p_member)
        {
            this.BorrowerUser = new Member().Set(p_member);
            return this;
        }

        public Project LoadBorrowerUser(P_Member p_member,U_Account u_account)
        {
            this.BorrowerUser = new Member().Set(p_member);
            this.BorrowerUser.Account = new Account().Set(u_account);
            return this;
        }

        public Project LoadProduct(P_Product p_product)
        {
            this.Product = new Product().Set(p_product);
            return this;
        }

        public Project LoadEnterprise(P_Enterprise p_enterprise)
        {
            this.Enterprise = new Enterprise().Set(p_enterprise);
            return this;
        }

        public Project LoadGuaranteeCompany(P_GuaranteeCompany p_guaranteeCompany)
        {
            this.GuaranteeCompany = new GuaranteeCompany().Set(p_guaranteeCompany);
            return this;
        }

        public Project LoadProjectPolicy(P_ProjectPolicy p_projectPolicy)
        {
            this.ProjectPolicy = new ProjectPolicy().Set(p_projectPolicy);
            return this;
        }

        public Project LoadPolicy(P_Policy p_policy)
        {
            this.Policy = new Policy().Set(p_policy);
            return this;
        }


        protected override void Covariant(P_Project model)
        {
            if (model != null)
            {
                this.BeginDate = model.BeginDate;
                this.BorrowerName = model.BorrowerName;
                this.CollateralOpinion = model.CollateralOpinion;
                this.CreateTime = model.CreateTime;
                this.CreateUser = model.CreateUser;
                this.CycleAmount = model.CycleAmount;
                this.EnterpriseID = model.EnterpriseID;
                this.EndDate = model.EndDate;
                this.Financing = model.Financing;
                this.GuaranteeCompanyID = model.GuaranteeCompanyID;
                this.ID = model.ID;
                this.IncreaseMoney = model.IncreaseMoney;
                this.MaxInvestment = model.MaxInvestment;
                this.MinInvestment = model.MinInvestment;
                this.ProductID = model.ProductID;
                this.ProductName = model.ProductName;
                this.ProjectCode = model.ProjectCode;
                this.ProjectDescription = model.ProjectDescription;
                this.ProjectTitle = model.ProjectTitle;
                this.ReleaseTime = model.ReleaseTime;
                this.FullScaleTime = model.FullScaleTime;
                this.LendingTime = model.LendingTime;
                this.BorrowInterestDate = model.BorrowInterestDate;
                this.RiskCotrolSummary = model.RiskCotrolSummary;
                this.Status = (ProjectStatus)model.Status;
                this.WaitDay = model.WaitDay;
                this.OriginalRate = model.OriginalRate;
                this.GuaranteeRate = model.GuaranteeRate;
            }
        }

        protected override P_Project Inverter(Project model)
        {
            if (model != null)
            {
                return new P_Project()
                {
                    BeginDate = model.BeginDate,
                    BorrowerName = model.BorrowerName,
                    CollateralOpinion = model.CollateralOpinion,
                    CreateTime = model.CreateTime,
                    CreateUser = model.CreateUser,
                    CycleAmount = model.CycleAmount,
                    EnterpriseID = model.EnterpriseID,
                    EndDate = model.EndDate,
                    Financing = model.Financing,
                    GuaranteeCompanyID = model.GuaranteeCompanyID,
                    ID = model.ID,
                    IncreaseMoney = model.IncreaseMoney,
                    MaxInvestment = model.MaxInvestment,
                    MinInvestment = model.MinInvestment,
                    ProductID = model.ProductID,
                    ProductName = model.ProductName,
                    ProjectCode = model.ProjectCode,
                    ProjectDescription = model.ProjectDescription,
                    ProjectTitle = model.ProjectTitle,
                    ReleaseTime = model.ReleaseTime,
                    FullScaleTime = model.FullScaleTime,
                    LendingTime = model.LendingTime,
                    BorrowInterestDate = model.BorrowInterestDate,
                    RiskCotrolSummary = model.RiskCotrolSummary,
                    Status = (int)model.Status,
                    WaitDay = model.WaitDay,
                    GuaranteeRate = model.GuaranteeRate,
                    OriginalRate = model.OriginalRate
                };
            }
            return null;
        }

        public Project Set(P_Project dbModel)
        {
            Covariant(dbModel);
            return this;
        }
    }
}
